package com.thanhloyal.exchanger;

import java.util.ArrayList;
import java.util.List;

import com.example.exchanger.R;

import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CurrencySpinnerAdapter extends ArrayAdapter<Currency> {

	private Context mContext = null;
	private ArrayList<Currency> currenciesList = new ArrayList<Currency>();
	
	Typeface britalicBold = null; 
	Typeface segoeui = null;
	
	public CurrencySpinnerAdapter(Context context, int resource,
			List<Currency> currenciesList) {
		super(context, resource, currenciesList);
		mContext = context;
		this.currenciesList = (ArrayList<Currency>) currenciesList;
		
		britalicBold = Typeface.createFromAsset(((Activity)mContext).getAssets(), "britalic_bold.TTF");
		segoeui = Typeface.createFromAsset(((Activity)mContext).getAssets(), "segoeuisb.ttf");
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return currenciesList.size();
	}

	@Override
	public Currency getItem(int position) {
		// TODO Auto-generated method stub
		return currenciesList.get(position);
	}
	
	@Override
	public int getPosition(Currency c) {
		for (int i = 0; i < currenciesList.size(); i++) {
			// Same currency code 
			// Log.v("Loyal", c.getCurrencyCode() + " : " +currenciesList.get(i).getCurrencyCode());
			if(c.getCurrencyCode().equalsIgnoreCase(currenciesList.get(i).getCurrencyCode())){
				return i;
			}
		}
		// Default
		return 0;
	}
	
	
	
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		NormalViewHolder normalViewHolder;

		if (convertView == null) {
			LayoutInflater inflater = ((Activity)mContext).getLayoutInflater();
			convertView = inflater.inflate(R.layout.currency_spinner_item, parent, false);
			ImageView flag = (ImageView)convertView.findViewById(R.id.currency_spinner_item_flag);
			TextView currencyCode = (TextView)convertView.findViewById(R.id.currency_spinner_item_code);
			
			normalViewHolder = new NormalViewHolder(flag, currencyCode);
			convertView.setTag(normalViewHolder);
		}
		else{
			normalViewHolder = (NormalViewHolder) convertView.getTag();
		}
		
		// Set up all information
		Currency cur = currenciesList.get(position);
		normalViewHolder.currencyCode.setText(cur.getCurrencyCode());
		normalViewHolder.currencyCode.setTypeface(britalicBold);
		normalViewHolder.flag.setImageBitmap(cur.getFlag());
		
		return convertView;
	}
	
	@Override
	public View getDropDownView(int position, View convertView, ViewGroup parent) {
		DropDownViewHolder dropDownViewHolder;
		if (convertView == null) {
			LayoutInflater inflater = ((Activity)mContext).getLayoutInflater();
			convertView = inflater.inflate(R.layout.currency_spinner_dropdown_item, parent, false);
			
			ImageView flag = (ImageView)convertView.findViewById(R.id.currency_spinner_dropdown_flag);
			TextView name = (TextView)convertView.findViewById(R.id.currency_spinner_dropdown_crcy_name);
			TextView code = (TextView)convertView.findViewById(R.id.currency_spinner_dropdown_crcy_code);
			
			dropDownViewHolder = new DropDownViewHolder(flag, code, name);
			
			convertView.setTag(dropDownViewHolder);
		} else {
			dropDownViewHolder = (DropDownViewHolder) convertView.getTag();
		}
		
		Currency cur = currenciesList.get(position);
		dropDownViewHolder.flag.setImageBitmap(cur.getFlag());
		dropDownViewHolder.currencyName.setText(cur.getCurrencyName());
		dropDownViewHolder.currencyName.setTypeface(segoeui);
		dropDownViewHolder.currencyCode.setText(cur.getCurrencyCode());
		dropDownViewHolder.currencyCode.setTypeface(britalicBold);
		
		return convertView;
	}

	private static class NormalViewHolder{
		public ImageView flag = null;
		public TextView currencyCode = null;
		
		public NormalViewHolder(ImageView flag, TextView currencyCode) {
			super();
			this.flag = flag;
			this.currencyCode = currencyCode;
		}
	}
	
	private static class DropDownViewHolder{
		public ImageView flag = null;
		public TextView currencyCode = null;
		public TextView currencyName = null;
		
		public DropDownViewHolder(ImageView flag, TextView currencyCode,
				TextView currencyName) {
			super();
			this.flag = flag;
			this.currencyCode = currencyCode;
			this.currencyName = currencyName;
		}
	}
}
